﻿ /*
 * ---------------------------------------------------
 * 作者: MTQ 
 * 日期： 2021/5/6 12:46:23 
 * ---------------------------------------------------
 */
    using System;                                    
    using System.Collections.Generic;                
    using System.Data;                               
    using System.Data.Entity;                        
    using System.Linq;                               
    using System.Web;                                
    using System.Web.Mvc;                            
    using MTQ.Model;             
    using MTQ.BLL;             
    using MTQ.IBLL;             
    using MTQ.Common;             
    using System.IO;                                                   
                                                       
                                                       
                                                       
                                                       
    namespace MTQ.Web.Controllers                    
    {                                                
                                                     
        public class Group_HostInfoController : BaseController 
        {                                            
                                                     
         private IGroup_HostInfoService  igroup_hostinfoService = new Group_HostInfoService();          
                                                     
                                                     
          //                                           
          // GET: //         
          [AccountAuth(Roles = "Group_HostInfo_Index")]  
          public ActionResult Index()      
          {                                          
                 return View();  
           }                              
                                                     
                                                     
          [AccountAuth(Roles = "Group_HostInfo_Index")]      
           public ActionResult Search()           
           {                                          
                                                     
                                                     
              ServertLayuiTable<Group_HostInfo> servertBootStrapTable = new ServertLayuiTable<Group_HostInfo>();     
                GetSearchData(ref servertBootStrapTable, true);                                      
                  
                                                     
               return Json(servertBootStrapTable, JsonRequestBehavior.AllowGet);   
           }                                          
                                                      
           private void GetSearchData(ref ServertLayuiTable<Group_HostInfo> servertBootStrapTable, bool page)                                            
          {                                            
                Group_HostInfoQuery igroup_hostinfoQuery = new Group_HostInfoQuery();                                      
                igroup_hostinfoQuery.Searchkey = GetRequestQueryString("keyword");                                             
                                                      
               igroup_hostinfoQuery.UserControlProjectNumber = UserControlProjectNumber;                                       
               if (page)                                       
               {                                       
                     servertBootStrapTable = igroup_hostinfoService.LoadLayuiSearchData(igroup_hostinfoQuery, Request);                                  
                }                                      
                else                                      
                {                                      
                      IQueryable<Group_HostInfo>  igroup_hostinfo = igroup_hostinfoService.GetIQueryable(igroup_hostinfoQuery);                                
                      servertBootStrapTable.data = igroup_hostinfo .ToList();                                
                       servertBootStrapTable.count = igroup_hostinfo .Count();                                
                }                                      
           }                                           
                                                      
                                                      
                                                      
                                                      
                                                      
                                                      
                                                      
                                                      
            //          
            // GET: /Group_HostInfo/Create          
            [AccountAuth(Roles = "Group_HostInfo_Create")]          
            public ActionResult Create()          
            {          
                Group_HostInfo group_hostinfo = new Group_HostInfo();     
                group_hostinfo.H_ID = System.Guid.NewGuid();     
                return View(group_hostinfo);          
            }          
                     
            //          
            // POST: /Group_HostInfo/Create          
            [HttpPost]          
            [AccountAuth(Roles = "Group_HostInfo_Create")]          
            public ActionResult Create(Group_HostInfo group_hostinfo)          
            {          
                ModelMessage listMsg = new ModelMessage(); 
                if (ModelState.IsValid) 
                { 
                    var result = igroup_hostinfoService.AddEntity(group_hostinfo); 
                    listMsg=new ModelMessage("ChangeResult", "m001", result != null ? "保存成功" : "保存失败"); 
                } 
                return Json(listMsg); 
            }  


         [AccountAuth(Roles = "Group_HostInfo_Edit")] 
         public ActionResult Edit(Guid? primarykey) 
         { 
             Group_HostInfo group_hostinfo = igroup_hostinfoService.LoadEntities(x => x.H_ID == primarykey).FirstOrDefault(); 
             if ( group_hostinfo == null) 
             { 
                 return HttpNotFound(); 
             } 
             return View( group_hostinfo); 
         } 
          
         [HttpPost] 
         [AccountAuth(Roles = "Group_HostInfo_Edit")] 
         public ActionResult Edit(Group_HostInfo formgroup_hostinfo) 
         { 
             ModelMessage listMsg = new ModelMessage(); 
             if (ModelState.IsValid) 
             { 
                 try 
                 { 
                     Group_HostInfo group_hostinfo = igroup_hostinfoService.LoadEntities(x => x.H_ID == formgroup_hostinfo.H_ID).First();
 group_hostinfo.H_ID= formgroup_hostinfo.H_ID;      
 group_hostinfo.H_Name_CN= formgroup_hostinfo.H_Name_CN;      
 group_hostinfo.H_Name_EN= formgroup_hostinfo.H_Name_EN;      
 group_hostinfo.H_IISURL_PC= formgroup_hostinfo.H_IISURL_PC;      
 group_hostinfo.H_IISURL_API= formgroup_hostinfo.H_IISURL_API;      
 group_hostinfo.H_IISURL_M= formgroup_hostinfo.H_IISURL_M;      
 group_hostinfo.H_ServerIP= formgroup_hostinfo.H_ServerIP;      
 group_hostinfo.H_ServerPort= formgroup_hostinfo.H_ServerPort;      
 group_hostinfo.H_DatabaseUser= formgroup_hostinfo.H_DatabaseUser;      
 group_hostinfo.H_DatabasePassword= formgroup_hostinfo.H_DatabasePassword;      
 group_hostinfo.H_DatabaseName= formgroup_hostinfo.H_DatabaseName;      
 group_hostinfo.H_Remark= formgroup_hostinfo.H_Remark;      
                     bool result = igroup_hostinfoService.UpdateEntity(group_hostinfo); 
                     listMsg=new ModelMessage("ChangeResult", "m001", result ? "保存成功" : "保存失败"); 
                 } 
                 catch (Exception e) 
                 { 
                     listMsg=new ModelMessage("ChangeResult", "m002", "保存失败"); 
                 } 
             } 
             return Json(listMsg); 
          
         } 
          
         [HttpPost] 
         [AccountAuth(Roles = "Group_HostInfo_Edit")] 
         public ActionResult ActivateSwich(string primarykeys, string activate = "N") 
         { 
             ModelMessage modelMessage = new ModelMessage(); 
          
             if (OperatorAvailable(primarykeys, activate)) 
             { 
                 modelMessage.Code = "m001"; 
                 modelMessage.Message = "修改成功"; 
             } 
             else 
             { 
                 modelMessage.Code = "m002"; 
             } 
             return Json(modelMessage, JsonRequestBehavior.AllowGet); 
         } 
          
         private bool OperatorAvailable(string primarykeys, string available) 
         { 
             List<Guid> listGroup_HostInfoGUID = primarykeys.Split(',').ToListGuid(); 
             List<Group_HostInfo> listGroup_HostInfo = igroup_hostinfoService.LoadEntities(x => listGroup_HostInfoGUID.Contains(x.H_ID)).ToList(); 
             bool updaterow = igroup_hostinfoService.DeleteListEntity(listGroup_HostInfo); 
             if (updaterow ) 
             { 
                 return false; 
             } 
             return true; 
         } 
                                                     
                                                     
         public ActionResult Export()                
          {                                           
                 ServertLayuiTable<Group_HostInfo> servertBootStrapTable = new ServertLayuiTable<Group_HostInfo>();                                     
                  GetSearchData(ref servertBootStrapTable, false);                                   
                  List<ExcelColumn> columnInfo = new List<ExcelColumn>();                                   
                                                     
                                                     
             columnInfo.Add(new ExcelColumn("H_ID", "H_ID"));                                        
             columnInfo.Add(new ExcelColumn("H_Name_CN", "H_Name_CN"));                                        
             columnInfo.Add(new ExcelColumn("H_Name_EN", "H_Name_EN"));                                        
             columnInfo.Add(new ExcelColumn("H_IISURL_PC", "H_IISURL_PC"));                                        
             columnInfo.Add(new ExcelColumn("H_IISURL_API", "H_IISURL_API"));                                        
             columnInfo.Add(new ExcelColumn("H_IISURL_M", "H_IISURL_M"));                                        
             columnInfo.Add(new ExcelColumn("H_ServerIP", "H_ServerIP"));                                        
             columnInfo.Add(new ExcelColumn("H_ServerPort", "H_ServerPort"));                                        
             columnInfo.Add(new ExcelColumn("H_DatabaseUser", "H_DatabaseUser"));                                        
             columnInfo.Add(new ExcelColumn("H_DatabasePassword", "H_DatabasePassword"));                                        
             columnInfo.Add(new ExcelColumn("H_DatabaseName", "H_DatabaseName"));                                        
             columnInfo.Add(new ExcelColumn("H_Remark", "H_Remark"));                                        
               byte[] btyBytes = null;                                        
                var filename = Server.MapPath("/") + @"DownloadDocument\" + System.Guid.NewGuid().ToString() + ".xlsx";     
                NPIOExportExcel.ExportExcelTest<Group_HostInfo>(servertBootStrapTable.data, filename, ref btyBytes, columnInfo, servertBootStrapTable.data.Count);                                      
                return File(filename, "text/comma-separated-values", "导出" + System.DateTime.Now.ToString("yyyyMMddHHmmss") + ".xls");                                      
                                                     
            }                                         
                                                     
            public ActionResult UploadFile(FormCollection form)                                          
            {                                         
                  string msg = string.Empty;                                   
                  if (Request.Files.Count == 0)                                   
                  {                                   
                        msg = "请选择文件";                              
                         return Json(msg);                             
                   }                                  
                    var file = Request.Files[0];                                  
                    if (file.ContentLength == 0)                                 
                    {                                 
                          msg = "请选择文件";                            
                           return Json(msg);                           
                    }                                 
                    file = Request.Files[0];                                 
                                                     
                   string virtualPath = Server.MapPath("/") + ("Attachment\\");                                          
                    Directory.CreateDirectory(virtualPath);                                  
                   string filename = System.Guid.NewGuid().ToString();//取得文件名字                                   
                  string path = virtualPath + filename;//获取存储的目标地址                                   
                  if (file.FileName.EndsWith(".xls"))                                   
                  {                                   
                      path += ".xls";                                
                   }                                  
                   else if (file.FileName.EndsWith(".xlsx"))                                  
                   {                                  
                        path += ".xlsx";                             
                    }                                 
                   else                                  
                   {                                  
                        msg = "只能上传Excel格式";                              
                        return Json(msg);                              
                   }                                  
                   file.SaveAs(path);                                  
                                                     
                    var result = Group_HostInfoService.ReadExcel(path, CurrentUserInfo.User_Id,ref msg);                                  
                      msg = result ? "成功": "失败, "+ msg;                                
                       return Json(new { success = result, Message = msg });                               
                      }                               
                                                     
                                                     
                                                     
                                                     
                                                     
                                                     
                                                     
                                                     
                                                     
                                                     
                                                     
                                                     
        }                                            
    }                                                
                                                     

